package com.yycx.module.bus.provider.handler.process.listener;

import com.yycx.common.constants.CommonConstants;
import com.yycx.module.bus.client.entity.BusOrder;
import com.yycx.module.bus.client.entity.BusOrderDetails;
import com.yycx.module.bus.client.enums.SaleTypeEnum;
import com.yycx.module.bus.provider.service.BusOrderDetailsService;
import com.yycx.module.bus.provider.service.BusOrderService;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.ExecutionListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * 订单审批结束监听器
 *
 * @author zyf
 */
@Component
public class OrderProcessEndListener implements ExecutionListener {
    @Resource
    private BusOrderService orderService;

    @Resource
    private BusOrderDetailsService orderDetailsService;


    @Override
    public void notify(DelegateExecution delegateExecution) {
        //获取业务主键
        String businessKey = delegateExecution.getProcessInstanceBusinessKey();
        BusOrder busOrder = orderService.getById(businessKey);
        busOrder.setOrderState(CommonConstants.INT_2);
        Long orderId = busOrder.getOrderId();
        //购货方资信
        Integer customBondType = busOrder.getCustomBondType();
        //订单类型
        Integer saleType = busOrder.getOrderType();
        //销售明细
        List<BusOrderDetails> orderDetailsList = orderDetailsService.selectByOrderId(orderId);
        //当订单是销售时
        if (saleType.equals(SaleTypeEnum.Xs.getCode())) {
            for (BusOrderDetails orderDetails : orderDetailsList) {
                //购货方全款时添加结算
                if (customBondType.equals(0)) {
                    orderDetailsService.updateHuikuanInfo(orderDetails,busOrder);
                }
            }
        }
        orderService.updateOrderState(busOrder.getOrderId(), CommonConstants.INT_2);
    }
}
